rMiW, EBImage
Last modified: 2021-10-27 15:35:44
Compiled: 2021-10-27 15:36:55
#Load packages
library(EBImage)
library(rMiW)
The displayed image (`Mouse01_Kid_x20_z0_RR01.png’) is an image of whole slide imaging (WSI) for observing the mouse P0 kidney tissue stained by H&E.
file <- system.file("extdata", "Mouse01_Kid_x20_z0_RR01.png", package="rMiW")
#Read image
Img <- EBImage::readImage(files = file)
str(Img)
#Visualization
EBImage::display(Img, method = "raster")
##Save as RDS
#saveRDS(Img, "./Img.Rds")
Figure 1: Mouse01_Kid_x20_z0_RR01.png
#Read image: delete the 4th element of 3th dimension
Img <- EBImage::readImage(files = file)
#Convert to the gray image
GrayImg <- toGrayScale(Img, mode = "luminance")
#Visualization
EBImage::display(GrayImg, method = "raster")
Figure 2: The gray image of Mouse01_Kid_x20_z0_RR01.png
#1% area size
Img10 <- EBImage::resize(Img, w = round(dim(Img)[1]*0.1, 0), filter = "bilinear")
#6% area size
Img25 <- EBImage::resize(Img, w = round(dim(Img)[1]*0.25, 0), filter = "bilinear")
#25% area size
Img50 <- EBImage::resize(Img, w = round(dim(Img)[1]*0.5, 0), filter = "bilinear")
#50% area size
Img70 <- EBImage::resize(Img, w = round(dim(Img)[1]*0.707, 0), filter = "bilinear")
#Visualization
par(mfrow=c(2,2))
EBImage::display(Img10, method = "raster")
text(x = dim(Img10)[1]/20, y = dim(Img10)[2]/10, label = "1% area size", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Img25, method = "raster")
text(x = dim(Img25)[1]/20, y = dim(Img25)[2]/10, label = "6% area size", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Img50, method = "raster")
text(x = dim(Img50)[1]/20, y = dim(Img50)[2]/10, label = "25% area size", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Img70, method = "raster")
text(x = dim(Img70)[1]/20, y = dim(Img70)[2]/10, label = "50% area size", adj = c(0,0), col = "black", cex = 1.5)
Figure 3: The resized images of Mouse01_Kid_x20_z0_RR01.png
#Transpose the image
ImgTrans <- EBImage::transpose(Img)
#Flip or flop the image
Imgflip <- EBImage::flip(Img)
Imgflop <- EBImage::flop(Img)
#Visualization
par(mfrow=c(2,2))
EBImage::display(Img, method = "raster")
text(x = dim(Img)[1]/20, y = dim(Img)[2]/10, label = "Original", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(ImgTrans, method = "raster")
text(x = dim(ImgTrans)[1]/20, y = dim(ImgTrans)[2]/10, label = "Transpose", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Imgflip, method = "raster")
text(x = dim(Imgflip)[1]/20, y = dim(Imgflip)[2]/10, label = "Flip", adj = c(0,0), col = "black", cex = 1.5)
EBImage::display(Imgflop, method = "raster")
text(x = dim(Imgflop)[1]/20, y = dim(Imgflop)[2]/10, label = "Flop", adj = c(0,0), col = "black", cex = 1.5)
Figure 4: The rotated images of Mouse01_Kid_x20_z0_RR01.png
k-means clustering is an unsupervised clustering technique to partition N observations into k clusters in which each observation belongs to the cluster with the nearest mean.
#Read image: delete the 4th element of 3th dimension
Img <- readRDS(system.file("extdata", "Mouse01_Kid_x20_z0_RR01.Rds", package="rMiW"))
str(Img)
#Resize 1024x1024 and perform 3 clustering
ImgClus3 <- Img2DClustering(x=Img, Cluster = 3, XY=1024)
#Visualize as a color image
rasterMiW(ImgClus3, method = "raster")
#Resize 1024x1024 and perform 4 clustering
ImgClus4 <- Img2DClustering(x=Img, Cluster = 4, XY=1024)
#Visualize as a color image
rasterMiW(ImgClus4)
## Formal class 'Image' [package "EBImage"] with 2 slots
## ..@ .Data : num [1:1257, 1:1038, 1:3] 0.902 0.906 0.906 0.902 0.906 ...
## ..@ colormode: int 2
## ..$ dim: int [1:3] 1257 1038 3
Figure 5: Results of three clustering with k-means for Mouse01_Kid_x20_z0_RR01.png
## R version 4.1.0 (2021-05-18)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Catalina 10.15.7
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
##
## locale:
## [1] ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/C/ja_JP.UTF-8/ja_JP.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocStyle_2.20.2
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 RColorBrewer_1.1-2 bslib_0.3.0
## [4] compiler_4.1.0 BiocManager_1.30.16 jquerylib_0.1.4
## [7] highr_0.9 base64enc_0.1-3 keras_2.6.0
## [10] bitops_1.0-7 tools_4.1.0 zeallot_0.1.0
## [13] digest_0.6.27 jsonlite_1.7.2 evaluate_0.14
## [16] lattice_0.20-44 png_0.1-7 rlang_0.4.11
## [19] Matrix_1.3-4 magick_2.7.3 yaml_2.2.1
## [22] parallel_4.1.0 xfun_0.26 fastmap_1.1.0
## [25] DiagrammeR_1.0.6.1 stringr_1.4.0 knitr_1.34
## [28] generics_0.1.0 sass_0.4.0 fftwtools_0.9-11
## [31] htmlwidgets_1.5.4 locfit_1.5-9.4 grid_4.1.0
## [34] reticulate_1.22 glue_1.4.2 R6_2.5.1
## [37] rMiW_0.99.1 jpeg_0.1-9 rmarkdown_2.11
## [40] bookdown_0.24 purrr_0.3.4 whisker_0.4
## [43] magrittr_2.0.1 tfruns_1.5.0 htmltools_0.5.2
## [46] BiocGenerics_0.38.0 assertthat_0.2.1 abind_1.4-5
## [49] colorspace_2.0-2 EBImage_4.34.0 tiff_0.1-8
## [52] tensorflow_2.6.0 stringi_1.7.4 visNetwork_2.0.9
## [55] RCurl_1.98-1.5